<template>
  <view 
    class="fu-grid-item"
    :class="{ 
      'fu-grid-item--square': grid.square,
      'fu-grid-item--border': grid.border
    }"
    :style="style"
    @click="onClick"
  >
    <view 
      class="fu-grid-item__content"
      :style="contentStyle"
    >
      <!-- 图标 -->
      <view v-if="icon || $slots.icon" class="fu-grid-item__icon">
        <slot name="icon">
          <text v-if="icon" class="fu-icon">{{ icon }}</text>
        </slot>
      </view>
      
      <!-- 文本 -->
      <view v-if="text || $slots.text" class="fu-grid-item__text">
        <slot name="text">{{ text }}</slot>
      </view>
      
      <!-- 自定义内容 -->
      <slot></slot>
    </view>
  </view>
</template>

<script>
/**
 * fu-grid-item 宫格项组件
 * @description 宫格组件的子组件
 * @property {String} icon - 图标
 * @property {String} text - 文字
 * @event {Function} click - 点击时触发
 */
export default {
  name: 'fu-grid-item',
  inject: ['grid'],
  props: {
    icon: {
      type: String,
      default: ''
    },
    text: {
      type: String,
      default: ''
    }
  },
  emits: ['click'],
  computed: {
    style() {
      const { columnNum, gutter } = this.grid
      const width = `${100 / columnNum}%`
      
      return {
        width,
        paddingRight: gutter ? `${gutter}rpx` : null,
        paddingTop: gutter ? `${gutter}rpx` : null
      }
    },
    contentStyle() {
      if (!this.grid.square) return {}
      
      return {
        paddingTop: '100%'
      }
    }
  },
  methods: {
    onClick(event) {
      this.$emit('click', event)
    }
  }
}
</script>

<style lang="scss" scoped>
.fu-grid-item {
  position: relative;
  box-sizing: border-box;
  
  &--border {
    &::after {
      content: '';
      position: absolute;
      box-sizing: border-box;
      top: 0;
      right: 0;
      width: 200%;
      height: 200%;
      transform: scale(0.5);
      transform-origin: right top;
      border-right: 2rpx solid var(--fu-border-color);
      border-bottom: 2rpx solid var(--fu-border-color);
      pointer-events: none;
    }
  }
  
  &--square {
    .fu-grid-item__content {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      box-sizing: border-box;
    }
  }
  
  &__content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 32rpx 16rpx;
    background-color: #fff;
    
    &:active {
      background-color: #f5f5f5;
    }
  }
  
  &__icon {
    margin-bottom: 16rpx;
    
    .fu-icon {
      font-family: 'fuIcon';
      font-size: 48rpx;
      color: var(--fu-text-color);
    }
  }
  
  &__text {
    color: var(--fu-text-color);
    font-size: 28rpx;
    line-height: 1.5;
    text-align: center;
  }
}
</style> 